﻿@{
    ViewData["Title"] = "角色管理";
}
    <div id="container">
        <div class="list-wall">
            <div class="layui-form list-search">
                <button type="button" class="layui-btn layui-btn-sm" data-type="add"><i class="layui-icon layui-icon-add-circle"></i>新增</button>
                <button type="button" class="layui-btn layui-btn-sm" data-type="del"><i class="layui-icon layui-icon-delete"></i>删除</button>
                <button type="button" class="layui-btn layui-btn-sm" data-type="toolMenu"><i class="layui-icon layui-icon-template"></i>菜单权限</button>
            </div>
            <table class="layui-table" id="tablist" lay-filter="tool"></table>
        </div>
        
        <script type="text/html" id="isEnableSwitch">
            <input type="checkbox" name="IsEnable" value="{{d.ID}}" lay-skin="switch" lay-text="启用|冻结" lay-filter="statusedit" {{ d.IsEnable?'checked':''}} disabled="">
        </script>
        <script type="text/html" id="tool">
            {{#  if(!d.IsSystem){ }}
            <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i> 修改</a>
            {{#  } }}
        </script>
    </div>
@section Scripts{
    <script type="text/javascript">

        layui.config({
            base: '/js/modules/'
        }).use(['table', 'layer', 'jquery', 'api'],
            function () {

                var table = layui.table,
                    layer = layui.layer,
                    $ = layui.jquery,
                    api = layui.api;

                table.render({
                    elem: '#tablist',
                    id: 'tables',
                    headers: api.getToken(),
                    url: '/Sys/Role/GetPages',
                    page: true,
                    page: { limits: [10, 20, 50, 100], groups: 8 },
                    cols: [
                        [
                            { type: 'checkbox', fixed: 'left' },
                            { field: 'Name', title: '角色名称', fixed: 'left' },
                            {
                                field: 'IsSystem', title: '是否系统管理员', templet: function (d) {
                                    if (d.IsSystem) {
                                        return '<span class="layui-badge-dot layui-bg-blue"></span>';
                                    } else {
                                        return '<span class="layui-badge-dot layui-bg-gray"></span>';
                                    }
                                }
                            },
                            { field: 'Sort', title: '排序' },
                            { field: 'IsEnable', title: '启用标记', templet: '#isEnableSwitch' },
                            { field: 'Creator', title: '创建人' },
                            { field: 'CreateTime', title: '创建时间', sort: true },
                            { field: 'Remark', title: '备注' },
                            { width: 100, title: '操作', templet: '#tool' }
                        ]
                    ]
                });

                var active = {
                    //刷新右侧表格
                    reload: function () {
                        table.reload('tables', {
                            page: { curr: 1 },
                        });
                    },
                    //给角色授权
                    toolMenu: function () {
                        var checkStatus = table.checkStatus('tables'), data = checkStatus.data;
                        if (data.length === 0) {
                            api.error("请选择要授权的角色");
                            return;
                        }
                        if (data.length > 1) {
                            api.error("请选择单个角色进行授权");
                            return;
                        }
                        var id = '', strCount = 0;
                        $.each(data, function (i, item) {
                            id = item.ID;
                            strCount++;
                        });

                        if (strCount == 0) {
                            api.warning('请选择要授权模块的角色');
                            return;
                        }
                        if (strCount > 1) {
                            api.warning('每次只能授权一个角色');
                            return;
                        }
                        api.Open('角色分配功能模块', '/Sys/Role/RoleMenu?id=' + id, '1000px', '650px', function () {

                        });
                    },
                    //弹出添加角色页面
                    add: function () {
                        api.Open('添加角色', '/Sys/Role/Modify', '620px', '450px', function () {
                            active.reload();
                        });
                    },
                    //删除右侧表格的项
                    del: function () {
                        var checkStatus = table.checkStatus('tables'), data = checkStatus.data;
                        if (data.length === 0) {
                            api.error("请选择要删除的项目~");
                            return;
                        }
                        var ids = '';
                        $.each(data, function (i, item) {
                            ids += item.ID + ",";
                        });
                        if (ids == '') {
                            api.error("请选择要删除的项目,超管状态不能删除~");
                            return;
                        }
                        layer.confirm('确定要执行批量删除吗？', function (index) {
                            layer.close(index);
                            var loadindex = layer.load(1, {
                                shade: [0.1, '#000']
                            });
                            api.ajax('Sys/Role/Delete/', 'post', { ids: ids }, function (res) {
                                layer.close(loadindex);
                                if (res.statusCode === 200) {
                                    active.reload();
                                    api.success(res.msg);
                                } else {
                                    api.error(res.msg);
                                }
                            });
                        });

                    }
                };

                //按钮点击事件
                $('.list-search .layui-btn').on('click', function () {
                    var type = $(this).data('type');
                    active[type] ? active[type].call(this) : '';
                });

                //监听工具条
                table.on('tool(tool)', function (obj) {
                    var data = obj.data;
                    if (obj.event === 'edit') {
                        api.Open('编辑角色', '/Sys/Role/Modify/?id=' + data.ID, '620px', '450px', function () {
                            active.reload();
                        })
                    }
                });
            });
    </script>
}